<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户ID" prop="userId">
      <el-input v-model="dataForm.userId" placeholder=""></el-input>
      <!-- <el-input placeholder="" v-model="dataForm.userId" :disabled="true"></el-input> -->
    </el-form-item>
    <el-form-item label="订单编号" prop="orderNumber">
      <el-input v-model="dataForm.orderNumber" placeholder=""></el-input>
       <!-- <el-input placeholder="" v-model="dataForm.userId" :disabled="true"></el-input> -->
    </el-form-item>
    <el-form-item label="优惠券ID" prop="couponId">
      <el-input v-model="dataForm.couponId" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <!-- <el-input v-model="dataForm.createTime" placeholder=""></el-input> -->
      <el-date-picker v-model="dataForm.createTime" type="datetime" placeholder="选择日期时间"></el-date-picker>
    </el-form-item>
    <el-form-item label="订单总额" prop="totalAmount">
      <el-input v-model="dataForm.totalAmount" placeholder="订单总额"></el-input>
    </el-form-item>
    <el-form-item label="运费金额" prop="freightAmount">
      <el-input v-model="dataForm.freightAmount" placeholder="运费金额"></el-input>
    </el-form-item>
    <el-form-item label="优惠券抵扣金额" prop="couponAmount">
      <el-input v-model="dataForm.couponAmount" placeholder="优惠券抵扣金额"></el-input>
    </el-form-item>
    <el-form-item label="积分抵扣金额" prop="integrationAmount">
      <el-input v-model="dataForm.integrationAmount" placeholder="积分抵扣金额"></el-input>
    </el-form-item>
    <el-form-item label="后台调整订单的折扣金额" prop="discountAmount">
      <el-input v-model="dataForm.discountAmount" placeholder="后台调整订单使用的折扣金额"></el-input>
    </el-form-item>
     <el-form-item label="应付总额" prop="payAmount">
      <el-input v-model="dataForm.payAmount" placeholder="应付总额"></el-input>
    </el-form-item>
    <el-form-item label="支付方式" prop="payType">
      <el-radio v-model="dataForm.payType" label="1">支付宝</el-radio>
      <el-radio v-model="dataForm.payType" label="2">微信</el-radio>
      <el-radio v-model="dataForm.payType" label="3">银联</el-radio>
      <el-radio v-model="dataForm.payType" label="4">货到付款</el-radio>
    </el-form-item>
      <!-- <el-input v-model="dataForm.payType" placeholder="支付方式【1->支付宝；2->微信；3->银联； 4->货到付款；】"></el-input> -->
    <el-form-item label="订单状态" prop="status">
      <!-- <el-input v-model="dataForm.status" placeholder="订单状态【0->待付款；1->待发货；2->已发货；3->已完成；4->已关闭；5->无效订单】"></el-input> -->
      <el-radio v-model="dataForm.status" label="1">待付款</el-radio>
      <el-radio v-model="dataForm.status" label="2">待发货</el-radio>
      <el-radio v-model="dataForm.status" label="3">已发货</el-radio>
      <el-radio v-model="dataForm.status" label="4">已完成</el-radio>
    </el-form-item>
    </el-form-item>
    <el-form-item label="收货信息" prop="receiveAddressId">
      <el-input v-model="dataForm.receiveAddressId" placeholder="收货信息"></el-input>
    </el-form-item>
    <!-- <el-form-item label="货物信息" prop="skuId">
      <el-input v-model="dataForm.skuId" placeholder=""></el-input>
    </el-form-item> -->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          userId: '',
          orderNumber: '',
          couponId: '',
          createTime: '',
          totalAmount: '',
          payAmount: '',
          freightAmount: '',
          couponAmount: '',
          integrationAmount: '',
          discountAmount: '',
          payType: '',
          status: '',
          receiveAddressId: ''
          // skuId: ''
        },
        dataRule: {
          userId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          orderNumber: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          couponId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          totalAmount: [
            { required: true, message: '订单总额不能为空', trigger: 'blur' }
          ],
          payAmount: [
            { required: true, message: '应付总额不能为空', trigger: 'blur' }
          ],
          freightAmount: [
            { required: true, message: '运费金额不能为空', trigger: 'blur' }
          ],
          couponAmount: [
            { required: true, message: '优惠券抵扣金额不能为空', trigger: 'blur' }
          ],
          integrationAmount: [
            { required: true, message: '积分抵扣金额不能为空', trigger: 'blur' }
          ],
          discountAmount: [
            { required: true, message: '后台调整订单使用的折扣金额不能为空', trigger: 'blur' }
          ],
          payType: [
            { required: true, message: '支付方式【1->支付宝；2->微信；3->银联； 4->货到付款；】不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '订单状态【0->待付款；1->待发货；2->已发货；3->已完成；4->已关闭；5->无效订单】不能为空', trigger: 'blur' }
          ],
          receiveAddressId: [
            { required: true, message: '收货信息不能为空', trigger: 'blur' }
          ]
          // skuId: [
          //   { required: true, message: '不能为空', trigger: 'blur' }
          // ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/order/orders/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userId = data.orders.userId
                this.dataForm.orderNumber = data.orders.orderNumber
                this.dataForm.couponId = data.orders.couponId
                this.dataForm.createTime = data.orders.createTime
                this.dataForm.totalAmount = data.orders.totalAmount
                this.dataForm.payAmount = data.orders.payAmount
                this.dataForm.freightAmount = data.orders.freightAmount
                this.dataForm.couponAmount = data.orders.couponAmount
                this.dataForm.integrationAmount = data.orders.integrationAmount
                this.dataForm.discountAmount = data.orders.discountAmount
                this.dataForm.payType = data.orders.payType
                this.dataForm.status = data.orders.status
                this.dataForm.receiveAddressId = data.orders.receiveAddressId
                // this.dataForm.skuId = data.orders.skuId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/order/orders/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'userId': this.dataForm.userId,
                'orderNumber': this.dataForm.orderNumber,
                'couponId': this.dataForm.couponId,
                'createTime': this.dataForm.createTime,
                'totalAmount': this.dataForm.totalAmount,
                'payAmount': this.dataForm.payAmount,
                'freightAmount': this.dataForm.freightAmount,
                'couponAmount': this.dataForm.couponAmount,
                'integrationAmount': this.dataForm.integrationAmount,
                'discountAmount': this.dataForm.discountAmount,
                'payType': this.dataForm.payType,
                'status': this.dataForm.status,
                'receiveAddressId': this.dataForm.receiveAddressId
                // 'skuId': this.dataForm.skuId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
